<?PHP
/**
* SAPID Data Abstraction Layer kernel extension.
* Defines DAL for core class.
*
* $Log: files.lib.php,v $
* Revision 1.3  2005/06/30 12:32:10  mephius
* fixed bug with page id overwrite while loading page data
*
* Revision 1.2  2005/06/14 14:26:23  mephius
* minor changes
*
*
* @package kernel
* @author $Author: mephius $
* @version 1.2 , 06.02.2005
* @since SAPID v.1.2.0
* @copyright (c) by Mephius
*/

/**
* SAPI Parser class.
* Extends core class.
* @package kernel
* @author Max Baryshnikov aka Mephius<mb@redgraphic.com>
* @version 1.2 , 07.12.2004
* @since SAPID v.1.2.0
* @copyright (c) by Mephius
*/

class sapi_parser extends sapi_core {
	
	/**
	* Class constructor. Just calls parent class canstructor.
	*
    * @param mixed $template Initial value of template to parse
	* @param array $env initial environmant array
	* @param string $handler this instanse name	
	* @param string $name this instanse name
	* @return object
	*/
	function sapi_parser($template, $env=false, $handler="", $name="sapi_env"){
		return parent::sapi_core($template, $env, $handler, $name);
	} //\\sapi_parser

	/**
	* Loads template for current instance of parser object.
	*
	* @param string $template
	*/
	function load_template($template) {
		if (is_file($template) or preg_match("/^http:\/\//", $template)) {
			$tp=@fopen($template, "r");
			if (!$tp) message_die("Couldn't open file <b>" . $template . "</b> for reading!");
			$this->template="";
			while (!feof($tp)) {
				$this->template.=fread($tp, 8192);
			}
			fclose($tp);
		}
	} //\\load_template

	/**
	* Loads data for current page
	*
	*/
	function load_pagedata(){
		if(!isset($this->env["page"]["ID"])) return;
		$filename=$GLOBALS["root_path"] . "usr/xml/content/" . $this->env["page"]["ID"] . ".xml";
		if (is_file($filename)) {
			$db = new sapi_vdb($filename);
			$pagedata=$db->get_data();
			unset($pagedata[1]["ID"]);
			$this->env["page"]=array_merge($this->env["page"], $pagedata[1]);
		}
	} //\\load_pagedata
	/**
	* Loads sapi definistions
	*
	* @param string $name
	* @param string $type
	* @return string
	*/
	function load_sapi($name, $type){
		$filename=$GLOBALS["root_path"] . "usr/xml/".$type."/" . $name . ".xml";
		if(is_file($filename)){
			$fp=@fopen($filename, "r");
			if (!$fp) $this->error="Couldn't open <br /><b>" . $filename . "</b> for reading!";
			else{
				$size=filesize($filename);
				$sapi=fread($fp, $size);
				fclose($fp);
			}
		} else $this->error="File does not exist:<br /><b>" . $filename . "</b>!";
		return $sapi;
	} //\\load_sapi


} //\\ sapi_parser

?>